﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Media3D;
using HelixToolkit.Wpf;
using PDBSmartMVVM.model;

namespace PDBSmartMVVM.viewmodel
{
    public class AtomViewModel : TreeViewItemViewModel
    {
        private Atom _atom;
        public Atom Atom
        {
            get { return _atom; }
            set
            {
                if (value != _atom)
                {
                    _atom = value;
                    OnPropertyChanged("Atom");
                }
            }
        }

        public ObservableCollection<Object> Objects { get; set; }

        public AtomViewModel(TreeViewItemViewModel parent, Atom atom, ObservableCollection<object> objects)
            : base(parent, false)
        {
            Atom = atom;
            Objects = objects;
            base.PropertyChanged += OnPropertyChanged;

        }

        private ModelUIElement3D _model;

        private void OnPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs)
        {
            if (propertyChangedEventArgs.PropertyName == "IsChecked")
            {
                var vm = (AtomViewModel)sender;
                if (vm.IsChecked == true)
                {
                    Atom.ModelUiElement3D = new ModelUIElement3D();
                    var me = new MeshBuilder();
                    var geometry = new GeometryModel3D();
                    me.AddSphere(Atom.Point, 0.08);
                    geometry.Geometry = me.ToMesh();
                    geometry.Material = Utils.GetColorByAtomType(Atom.AtomType);
                    Atom.ModelUiElement3D.Model = geometry;
                    Atom.ModelUiElement3D.MouseLeftButtonDown += ModelUiElement3DOnMouseLeftButtonDown;
                    Objects.Add((Atom.ModelUiElement3D));
                }
                else
                {
                    Objects.Remove(_model);
                    Atom.ModelUiElement3D = null;
                }
            }
        }

        private void ModelUiElement3DOnMouseLeftButtonDown(object sender, MouseButtonEventArgs mouseButtonEventArgs)
        {

            Parent.IsCalculated = IsCalculated == false;

            ChangeCalculated();

        }

        public void ChangeCalculated()
        {
            foreach (AtomViewModel child in Parent.Children)
            {
                child.IsCalculated = Parent.IsCalculated;
                var me = new MeshBuilder();
                var geometry = new GeometryModel3D();
                if (Parent.IsCalculated)
                {
                    me.AddSphere(child.Atom.Point, 0.15);
                    geometry.Geometry = me.ToMesh();
                    geometry.Material = Utils.SelectedMaterial;
                    if (child.Atom.ModelUiElement3D == null)
                    {
                        child.Atom.ModelUiElement3D = new ModelUIElement3D();
                        Parent.IsChecked = true;
                    }
                    child.Atom.ModelUiElement3D.Model = geometry;
                }
                else
                {
                    me.AddSphere(child.Atom.Point, 0.08);
                    geometry.Geometry = me.ToMesh();
                    geometry.Material = Utils.GetColorByAtomType(child.Atom.AtomType);
                    if (child.Atom.ModelUiElement3D == null)
                        child.Atom.ModelUiElement3D = new ModelUIElement3D();
                    child.Atom.ModelUiElement3D.Model = geometry;
                }
            }
        }
    }
}
